package com.example.freshmenclient;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Response;
import com.example.freshmenclient.bean.ClassMateDetailBean;
import com.example.freshmenclient.util.GainDataUtil;
import com.example.freshmenclient.util.UrlUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class ClassMateDetailActivity extends AppCompatActivity {

    private static final String TAG = "ClassMateDetailActivity";
    private TextView tvClassMateName;
    private TextView tvClassMatePhone;
    private TextView tvClassMateDept;
    private TextView tvClassMateMajor;
    private TextView tvClassMateEmail;
    private ClassMateDetailBean classMateDetailBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_mate_detail);
        initView();
        initData();
    }

    private void initData() {
        String url = UrlUtil.basePath + "freshmenserver/classmate/detail";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", UrlUtil.userId);

            GainDataUtil.gainDataForPost(this, url, jsonObject
                    , new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                            Log.e(TAG, "onResponse: " + jsonObject);
                            Gson gson = new Gson();
                            classMateDetailBean = gson.fromJson(jsonObject.toString(), ClassMateDetailBean.class);

                            tvClassMateName.setText(classMateDetailBean.getData().getName());
                            tvClassMatePhone.setText(classMateDetailBean.getData().getTelephone());
                            tvClassMateDept.setText(classMateDetailBean.getData().getDepartment());
                            tvClassMateMajor.setText(classMateDetailBean.getData().getMajor());
                            tvClassMateEmail.setText(classMateDetailBean.getData().getEmail());
                        }
                    }, null);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        tvClassMateName = findViewById(R.id.tv_classmat_name);
        tvClassMatePhone = findViewById(R.id.tv_classmat_phone);
        tvClassMateDept = findViewById(R.id.tv_classmat_dept);
        tvClassMateMajor = findViewById(R.id.tv_classmat_major);
        tvClassMateEmail = findViewById(R.id.tv_classmat_email);
    }

    public void back(View view) {
        finish();
    }
}
